stack: Derive from GtkWidget
authorMatthias Clasen <mclasen@redhat.com>
Thu, 7 May 2020 20:04:42 +0000 (16:04 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 12 May 2020 02:21:39 +0000 (22:21 -0400)
gtk/a11y/gtkstackaccessible.c
gtk/a11y/gtkstackaccessible.h
gtk/gtkstack.c
gtk/gtkstack.h

index 240daad2645f0ea1e5b69ea53e121ba368e8846c..23b4e13762e412212d8fde2e141673796705781d 100644 (file)
@@ -23,7 +23,7 @@
 #include "gtkwidgetprivate.h"
 
 
-G_DEFINE_TYPE (GtkStackAccessible, gtk_stack_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE)
+G_DEFINE_TYPE (GtkStackAccessible, gtk_stack_accessible, GTK_TYPE_WIDGET_ACCESSIBLE)
 
 static AtkObject*
 gtk_stack_accessible_ref_child (AtkObject *obj,
@@ -64,16 +64,9 @@ static void
 gtk_stack_accessible_class_init (GtkStackAccessibleClass *klass)
 {
   AtkObjectClass *class                        = ATK_OBJECT_CLASS (klass);
-  GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
 
   class->get_n_children = gtk_stack_accessible_get_n_children;
   class->ref_child      = gtk_stack_accessible_ref_child;
-  /*
-   * As we report the stack as having only the visible child,
-   * we are not interested in add and remove signals
-   */
-  container_class->add_gtk    = NULL;
-  container_class->remove_gtk = NULL;
 }
 
 static void
index 11862bbc85fcdba66c7727af2b948b79fbc227b2..6254c338025d2c6b27ced661101d338115442a98 100644 (file)
@@ -22,7 +22,7 @@
 #error "Only <gtk/gtk-a11y.h> can be included directly."
 #endif
 
-#include <gtk/a11y/gtkcontaineraccessible.h>
+#include <gtk/a11y/gtkwidgetaccessible.h>
 
 G_BEGIN_DECLS
 
@@ -38,12 +38,12 @@ typedef struct _GtkStackAccessibleClass   GtkStackAccessibleClass;
 
 struct _GtkStackAccessible
 {
-  GtkContainerAccessible parent;
+  GtkWidgetAccessible parent;
 };
 
 struct _GtkStackAccessibleClass
 {
-  GtkContainerAccessibleClass parent_class;
+  GtkWidgetAccessibleClass parent_class;
 };
 
 GDK_AVAILABLE_IN_ALL
index 9feb4f8d0dc999c90c994d8112bc18b43e9478f2..20c186d82af8e4ef3a176665fa498e5beb9512f7 100644 (file)
  */
 
 struct _GtkStack {
-  GtkContainer parent_instance;
+  GtkWidget parent_instance;
 };
 
 typedef struct _GtkStackClass GtkStackClass;
 struct _GtkStackClass {
-  GtkContainerClass parent_class;
+  GtkWidgetClass parent_class;
 };
 
 typedef struct {
@@ -157,7 +157,7 @@ typedef struct {
 
 static void gtk_stack_buildable_interface_init (GtkBuildableIface *iface);
 
-G_DEFINE_TYPE_WITH_CODE (GtkStack, gtk_stack, GTK_TYPE_CONTAINER,
+G_DEFINE_TYPE_WITH_CODE (GtkStack, gtk_stack, GTK_TYPE_WIDGET,
                          G_ADD_PRIVATE (GtkStack)
                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                 gtk_stack_buildable_interface_init))
@@ -552,11 +552,12 @@ gtk_stack_pages_new (GtkStack *stack)
   return pages;
 }
 
-static void     gtk_stack_add                            (GtkContainer  *widget,
-                                                          GtkWidget     *child);
-static void     gtk_stack_forall                         (GtkContainer  *container,
-                                                          GtkCallback    callback,
-                                                          gpointer       callback_data);
+static GtkStackPage *gtk_stack_add_internal (GtkStack *stack,
+                                             GtkWidget  *child,
+                                             const char *name,
+                                             const char *title);
+
+static GtkSizeRequestMode gtk_stack_get_request_mode (GtkWidget *widget);
 static void     gtk_stack_compute_expand                 (GtkWidget     *widget,
                                                           gboolean      *hexpand,
                                                           gboolean      *vexpand);
@@ -589,6 +590,8 @@ static void     gtk_stack_unschedule_ticks               (GtkStack      *stack);
 static void     gtk_stack_add_page                       (GtkStack     *stack,
                                                           GtkStackPage *page);
 
+static GtkBuildableIface *parent_buildable_iface;
+
 static void
 gtk_stack_buildable_add_child (GtkBuildable *buildable,
                                GtkBuilder   *builder,
@@ -598,14 +601,16 @@ gtk_stack_buildable_add_child (GtkBuildable *buildable,
   if (GTK_IS_STACK_PAGE (child))
     gtk_stack_add_page (GTK_STACK (buildable), GTK_STACK_PAGE (child));
   else if (GTK_IS_WIDGET (child))
-    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
+    gtk_stack_add_internal (GTK_STACK (buildable), GTK_WIDGET (child), NULL, NULL);
   else
-    g_warning ("Can't add a child of type '%s' to '%s'", G_OBJECT_TYPE_NAME (child), G_OBJECT_TYPE_NAME (buildable));
+    parent_buildable_iface->add_child (buildable, builder, child, type);
 }
 
 static void
 gtk_stack_buildable_interface_init (GtkBuildableIface *iface)
 {
+  parent_buildable_iface = g_type_interface_peek_parent (iface);
+
   iface->add_child = gtk_stack_buildable_add_child;
 }
 
@@ -613,22 +618,18 @@ static void stack_remove (GtkStack  *stack,
                           GtkWidget *child,
                           gboolean   in_dispose);
 
-static void
-remove_child (GtkWidget *child, gpointer user_data)
-{
-  stack_remove (GTK_STACK (user_data), child, TRUE);
-}
-
 static void
 gtk_stack_dispose (GObject *obj)
 {
   GtkStack *stack = GTK_STACK (obj);
   GtkStackPrivate *priv = gtk_stack_get_instance_private (stack);
+  GtkWidget *child;
 
   if (priv->pages)
     g_list_model_items_changed (G_LIST_MODEL (priv->pages), 0, g_list_length (priv->children), 0);
 
-  gtk_container_foreach (GTK_CONTAINER (obj), remove_child, obj);
+  while ((child = gtk_widget_get_first_child (GTK_WIDGET (stack))))
+    stack_remove (stack, child, TRUE);
 
   G_OBJECT_CLASS (gtk_stack_parent_class)->dispose (obj);
 }
@@ -734,7 +735,6 @@ gtk_stack_class_init (GtkStackClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
-  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
 
   object_class->get_property = gtk_stack_get_property;
   object_class->set_property = gtk_stack_set_property;
@@ -745,10 +745,7 @@ gtk_stack_class_init (GtkStackClass *klass)
   widget_class->snapshot = gtk_stack_snapshot;
   widget_class->measure = gtk_stack_measure;
   widget_class->compute_expand = gtk_stack_compute_expand;
-
-  container_class->add = gtk_stack_add;
-  container_class->remove = gtk_stack_remove;
-  container_class->forall = gtk_stack_forall;
+  widget_class->get_request_mode = gtk_stack_get_request_mode;
 
   /**
    * GtkStack:hhomogeneous:
@@ -1323,12 +1320,6 @@ stack_child_visibility_notify_cb (GObject    *obj,
     }
 }
 
-static GtkStackPage *
-gtk_stack_add_internal (GtkStack *stack,
-                        GtkWidget  *child,
-                        const char *name,
-                        const char *title);
-
 /**
  * gtk_stack_add_titled:
  * @stack: a #GtkStack
@@ -1377,15 +1368,6 @@ gtk_stack_add_named (GtkStack   *stack,
   return gtk_stack_add_internal (stack, child, name, NULL);
 }
 
-static void
-gtk_stack_add (GtkContainer *container,
-               GtkWidget    *child)
-{
-  GtkStack *stack = GTK_STACK (container);
-
-  gtk_stack_add_internal (stack, child, NULL, NULL);
-}
-
 static GtkStackPage *
 gtk_stack_add_internal (GtkStack   *stack,
                         GtkWidget  *child,
@@ -2002,26 +1984,6 @@ gtk_stack_set_visible_child_full (GtkStack               *stack,
     set_visible_child (stack, child_info, transition, priv->transition_duration);
 }
 
-static void
-gtk_stack_forall (GtkContainer *container,
-                  GtkCallback   callback,
-                  gpointer      callback_data)
-{
-  GtkStack *stack = GTK_STACK (container);
-  GtkStackPrivate *priv = gtk_stack_get_instance_private (stack);
-  GtkStackPage *child_info;
-  GList *l;
-
-  l = priv->children;
-  while (l)
-    {
-      child_info = l->data;
-      l = l->next;
-
-      (* callback) (child_info->widget, callback_data);
-    }
-}
-
 static void
 gtk_stack_compute_expand (GtkWidget *widget,
                           gboolean  *hexpand_p,
@@ -2057,6 +2019,40 @@ gtk_stack_compute_expand (GtkWidget *widget,
   *vexpand_p = vexpand;
 }
 
+static GtkSizeRequestMode
+gtk_stack_get_request_mode (GtkWidget *widget)
+{
+  GtkWidget *w;
+  int wfh = 0, hfw = 0;
+
+  for (w = gtk_widget_get_first_child (widget);
+       w != NULL;
+       w = gtk_widget_get_next_sibling (w))
+    {
+      GtkSizeRequestMode mode = gtk_widget_get_request_mode (w);
+
+      switch (mode)
+        {
+        case GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH:
+          hfw ++;
+          break;
+        case GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT:
+          wfh ++;
+          break;
+        case GTK_SIZE_REQUEST_CONSTANT_SIZE:
+        default:
+          break;
+        }
+    }
+
+  if (hfw == 0 && wfh == 0)
+    return GTK_SIZE_REQUEST_CONSTANT_SIZE;
+  else
+    return wfh > hfw ?
+        GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT :
+        GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
+}
+
 static void
 gtk_stack_snapshot_crossfade (GtkWidget   *widget,
                               GtkSnapshot *snapshot)
index 23f22a6fb8bcdf6ce5b5eac00924fbe948f29c02..6e52039de6069a1e2fc8504115ed21ba6cdebba9 100644 (file)
@@ -26,7 +26,7 @@
 #error "Only <gtk/gtk.h> can be included directly."
 #endif
 
-#include <gtk/gtkcontainer.h>
+#include <gtk/gtkwidget.h>
 #include <gtk/gtkselectionmodel.h>
 
 G_BEGIN_DECLS